﻿Imports System.Text
Imports NewHeaderSystems.Archive.Data

Public Class ConfirmRenderer

    Public Function Render(ByVal confirmID As Integer) As String
        Return Render(New Integer() {confirmID}, String.Empty)
    End Function

    Public Function Render(ByVal confirmList() As Integer, ByVal seperator As String) As String
        Dim confirmText As New StringBuilder
        For Each confirmID As Integer In confirmList
            Dim confirm As Confirm = DataScript.GetConfirm(confirmID)
            confirmText.Append(CreateConfirmText(confirm))
            If Not String.IsNullOrEmpty(seperator) Then
                confirmText.AppendLine()
                confirmText.AppendLine(seperator)
            End If
        Next
        Return confirmText.ToString()
    End Function

    Private Function CreateConfirmText(ByVal confirm As Confirm) As String
        Try

            Dim confirmText As String
            Dim strScratch As String

            confirmText = confirm.HeadingLine & vbCrLf

            confirmText = confirmText & " INST" & PadLeft(confirm.InstitutionID, 18) & _
                          Space(1) & PadRight(confirm.InstitutionName, 30, 35) & "A/C " & confirm.AccountID & vbCrLf

            confirmText = confirmText & " BRK-DLR"

            strScratch = PadLeft(confirm.BkrDlrNum, 15)

            confirmText = confirmText & strScratch & Space(1) & PadRight(confirm.BkrDlrName, 35) & _
                          "A/C " & confirm.BkrDlrAccountNum & vbCrLf

            If confirm.Side = "SELL" Then
                confirmText = confirmText & " REC-BKR"
            Else
                confirmText = confirmText & " DEL-BKR"
            End If

            strScratch = PadLeft(confirm.ClrBkrNum, 15)

            confirmText = confirmText & strScratch & Space(1) & PadRight(confirm.ClrBkrName, 35) & "A/C " & vbCrLf

            confirmText = confirmText & " ID AGT"

            strScratch = PadLeft(confirm.AgtNum, 16)

            confirmText = confirmText & strScratch & Space(1) & PadRight(confirm.AgtName, 35) & "A/C " & confirm.AgtAccountNum & vbCrLf

            If confirm.Side = "SELL" Then
                confirmText = confirmText & " DEL AGT"
            Else
                confirmText = confirmText & " REC AGT"
            End If

            strScratch = PadLeft(confirm.ClrAgtNum, 15)

            confirmText = confirmText & strScratch & Space(1) & PadRight(confirm.ClrAgtName, 35) & "A/C " & vbCrLf & vbCrLf

            strScratch = " INT PARTY 1" & PadLeft(confirm.InterestedParty1Name, 11)
            confirmText = confirmText & strScratch & Space(1) & PadRight(confirm.InterestedParty1, 35) & "A/C " & vbCrLf

            strScratch = " INT PARTY 2" & PadLeft(confirm.InterestedParty2Name, 11)
            confirmText = confirmText & strScratch & Space(1) & PadRight(confirm.InterestedParty2, 35) & "A/C " & vbCrLf

            strScratch = " INT PARTY 3" & PadLeft(confirm.InterestedParty2Name, 11)
            confirmText = confirmText & strScratch & Space(1) & PadRight(confirm.InterestedParty2, 35) & "A/C " & vbCrLf & vbCrLf

            confirmText = confirmText & " DTC CONTROL# " & PadLeft(confirm.DTCControlNumber, 14) & vbCrLf
            confirmText = confirmText & " BKR-DLR CONF# " & PadLeft(confirm.ControlNum, 13) & vbCrLf & vbCrLf

            confirmText = confirmText & " TRAN CODE " & Space(2) & confirm.Side & vbCrLf
            confirmText = confirmText & " TRADE DATE" & Space(2) & confirm.TradeDate & _
                          Space(2) & "SETTLE DATE   " & confirm.SettlementDate & _
                          Space(2) & "ISSUE DATE" & vbCrLf
            confirmText = confirmText & " SECTYPE" & Space(5) & PadRight(confirm.ExpSecurityType, 11, 12) & _
                          "INTEREST RATE" & Space(13) & "MATURITY DATE" & vbCrLf

            ' Interest Rate & maturity date not on Advent XML
            confirmText = confirmText & " POOL#" & Space(19) & "MOODYS" & "" & Space(20) & "INT PAY DATE" & vbCrLf
            confirmText = confirmText & Space(25) & "S&P" & vbCrLf & vbCrLf

            confirmText = confirmText & " CUSIP" & Space(6) & confirm.SecurityID & "  (" & confirm.TickerSymbol & ")" & vbCrLf
            confirmText = confirmText & " DESC-1" & Space(5) & confirm.SecurityDescription & vbCrLf
            confirmText = confirmText & " DESC-2" & Space(5) & confirm.SecurityDescription2 & vbCrLf
            confirmText = confirmText & " DESC-3" & Space(5) & confirm.SecurityDescription3 & vbCrLf & vbCrLf


            confirmText = confirmText & " CONFIRM DATE" & PadLeft(confirm.ActivityDate, 15) & Space(15) & "CONFIRM TIME" & _
                          Space(2) & confirm.ActivityTime & vbCrLf & vbCrLf
            confirmText = confirmText & " SHR-FV" & PadLeft(confirm.Units, 21) & vbCrLf
            confirmText = confirmText & " CUR-FV                   NA" & vbCrLf
            confirmText = confirmText & " PRICE IN USD" & PadLeft(confirm.PricePerShare, 15) & vbCrLf
            If confirm.NetPrice = "" Then
                confirm.NetPrice = "NA"
            End If
            confirmText = confirmText & " NET PRICE" & PadLeft(confirm.NetPrice, 18) & vbCrLf
            confirmText = confirmText & " PRINCIPAL" & PadLeft(confirm.Principal, 18) & vbCrLf
            If confirm.Fee = "" Then
                confirm.Fee = "NA"
            End If
            confirmText = confirmText & " SEC-REG-SHP FEE" & PadLeft(confirm.Fee, 12) & vbCrLf
            If confirm.AccruedInterest = "" Then
                confirm.AccruedInterest = "NA"
            End If
            confirmText = confirmText & " INTEREST" & PadLeft(confirm.AccruedInterest, 19) & vbCrLf
            confirmText = confirmText & " COUNTRY-FED TAX        0.00" & vbCrLf
            confirmText = confirmText & " PROV-ST-LOC TAX        0.00" & vbCrLf
            If confirm.Commission = "" Then
                confirm.Commission = "NA"
            End If
            confirmText = confirmText & " COMMISSION" & PadLeft(confirm.Commission, 17) & vbCrLf
            If confirm.OtherCharges = "" Then
                confirm.OtherCharges = "NA"
            End If
            confirmText = confirmText & " OTHER CHARGES" & PadLeft(confirm.OtherCharges, 14) & vbCrLf & vbCrLf
            confirmText = confirmText & " NET AMT" & PadLeft(confirm.NetAmount, 20) & vbCrLf & vbCrLf

            If Len(confirm.InterestedParty1) > 1 Then
                confirmText = confirmText & Space(1) & confirm.InterestedParty1 & vbCrLf
            End If
            If Len(confirm.InterestedParty2) > 1 Then
                confirmText = confirmText & Space(1) & confirm.InterestedParty2 & vbCrLf
            End If

            If Len(confirm.SpecialInstr1) > 1 Then
                confirmText = confirmText & Space(1) & confirm.SpecialInstr1 & vbCrLf
            End If
            If Len(confirm.SpecialInstr2) > 1 Then
                confirmText = confirmText & Space(1) & confirm.SpecialInstr2 & vbCrLf
            End If
            If Len(confirm.SpecialInstr3) > 1 Then
                confirmText = confirmText & Space(1) & confirm.SpecialInstr3 & vbCrLf
            End If
            If Len(confirm.SpecialInstr4) > 1 Then
                confirmText = confirmText & Space(1) & confirm.SpecialInstr4 & vbCrLf
            End If
            If Len(confirm.SpecialInstr5) > 1 Then
                confirmText = confirmText & Space(1) & confirm.SpecialInstr5 & vbCrLf
            End If

            confirmText = confirmText & vbCrLf

            confirmText = confirmText & Space(1) & "Account: " & confirm.AccountID & vbCrLf
            confirmText = confirmText & Space(1) & "Symbol:  " & confirm.TickerSymbol & vbCrLf

            Return confirmText

        Catch ex As Exception
            Core.Log.Write("Error rendering confirm", ex)
        End Try
        Return "An error occured while rendering this confirm. Please check the Windows Event Log for details."
    End Function

    Private Function PadRight(ByVal text As String, ByVal totalLength As Integer) As String
        Return PadRight(text, totalLength, totalLength)
    End Function

    ''' <summary>
    ''' Adds spacing on the right to fill and trims the text if it exceeds the maxlength
    ''' </summary>
    Private Function PadRight(ByVal text As String, ByVal maxTextLength As Integer, ByVal totalLength As Integer) As String
        Return Left(Trim(text), maxTextLength).PadRight(totalLength)
    End Function

    Private Function PadLeft(ByVal text As String, ByVal totalLength As Integer) As String
        Return PadLeft(text, totalLength, totalLength)
    End Function

    ''' <summary>
    ''' Adds spacing on the right to fill and trims the text if it exceeds the maxlength
    ''' </summary>
    Private Function PadLeft(ByVal text As String, ByVal maxTextLength As Integer, ByVal totalLength As Integer) As String
        Return Left(Trim(text), maxTextLength).PadLeft(totalLength)
    End Function
End Class
